package com.ruoyi.web.controller.shouhou;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.ShouhouInfo;
import com.ruoyi.system.domain.ShouhouParts;
import com.ruoyi.system.domain.ShouhouWuliu;
import com.ruoyi.system.service.IEpmShouhouMaintainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 售后信息表及相关处理controller
 */


@RestController
@RequestMapping("/Empment/shouhou")
public class EpmShouhouMaintainController extends BaseController {
    @Autowired
    private IEpmShouhouMaintainService iEpmShouhouMaintainService;

    /**
     * 查询售后信息表
     */
    @PreAuthorize("@ss.hasPermi('Empment:maintain:list')")
    @GetMapping("/maintain/list")
    public TableDataInfo list(ShouhouInfo shouhouInfo) {
        startPage();
        List<ShouhouInfo> list = iEpmShouhouMaintainService.list(shouhouInfo);
        return getDataTable(list);
    }

    /**
     * 新增数据
     */
    @PreAuthorize("@ss.hasPermi('Empment:maintain:insert')")
    @PostMapping("/maintain/insert")
    public AjaxResult insert(@RequestBody ShouhouInfo shouhouInfo) {
        try {
            iEpmShouhouMaintainService.insertShouhouInfo(shouhouInfo);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error("SN值已存在，请检查");
        }
    }

    /**
     * 修改数据
     */
    @PreAuthorize("@ss.hasPermi('Empment:maintain:upadte')")
    @PutMapping("/maintain/update")
    public AjaxResult update(@RequestBody ShouhouInfo shouhouInfo) {
        iEpmShouhouMaintainService.update(shouhouInfo);
        return AjaxResult.success();
    }

    /**
     * 进行维修
     */
    @PreAuthorize("@ss.hasPermi('Empment:maintain:weixiu')")
    @PostMapping("/maintain/weixiu")
    public AjaxResult weixiu(@RequestBody ShouhouInfo shouhouInfo) {
        iEpmShouhouMaintainService.updateShouhouWeixiu(shouhouInfo);
        return AjaxResult.success();
    }


    /**
     * 发货操作
     * 由于插入操作比较容易进行且不易出错，因此这里优先操作查询操作，最后进行插入
     * 1. 连表进行查询获取所有数据
     * 2. 删除售后两张表的数据
     * 3. 插入物流信息
     */

    // 查询兼删除
    @PreAuthorize("@ss.hasPermi('Empment:maintain:send')")
    @DeleteMapping("/maintain/delete/{snId}")
    public AjaxResult send(@PathVariable String snId) {
        iEpmShouhouMaintainService.send(snId);
        return AjaxResult.success();
    }

    // 记录物流表信息
    @PreAuthorize("@ss.hasPermi('Empment:maintain:send')")
    @PostMapping("/maintain/wuliu/insert")
    public AjaxResult insert(@RequestBody ShouhouWuliu shouhouWuliu) {
        iEpmShouhouMaintainService.insertShouhouWuliu(shouhouWuliu);
        return AjaxResult.success();
    }

    /**
     * 更新七天失效情况
     */
    @PreAuthorize("@ss.hasPermi('Empment:maintain:loadList')")
    @PutMapping("/maintain/loadList")
    public AjaxResult loadList(ShouhouInfo shouhouInfo) {
        iEpmShouhouMaintainService.loadList(shouhouInfo);
        return AjaxResult.success();
    }




    /**
     * 配件信息相关操作：增删查
     */

    // 查询配件信息
    @PreAuthorize("@ss.hasPermi('Empment:maintain:partsQuery')")
    @GetMapping("/maintain/partsQuery/{snId}")
    public TableDataInfo partsQuery(@PathVariable String snId) {
         List<ShouhouParts> shouhouPartsList =iEpmShouhouMaintainService.partsQuery(snId);
        return getDataTable(shouhouPartsList);
    }

    // 新增配件
    @PreAuthorize("@ss.hasPermi('Empment:maintain:partsAdd')")
    @PostMapping("/maintain/partsAdd")
    public AjaxResult partsAdd(@RequestBody ShouhouParts shouhouParts) {
        if (shouhouParts.getOriginPartsSnId() == null || shouhouParts.getOriginPartsSnId().isEmpty()) {
            return AjaxResult.error("原配件SN码不能为空");
        }
        if (shouhouParts.getNewPartsSnId() == null || shouhouParts.getNewPartsSnId().isEmpty()) {
            return AjaxResult.error("新配件SN码不能为空");
        }
        // 检查二者是否相同
        if (shouhouParts.getOriginPartsSnId().equals(shouhouParts.getNewPartsSnId())) {
            return AjaxResult.error("原配件SN码和新配件SN码不能相同");
        }
        iEpmShouhouMaintainService.partsAdd(shouhouParts);
        return AjaxResult.success();
    }

    // 删除配件
    @PreAuthorize("@ss.hasPermi('Empment:maintain:partsDelete')")
    @DeleteMapping("/maintain/partsDelete/{id}")
    public AjaxResult partsDelete(@PathVariable Integer id) {
        iEpmShouhouMaintainService.partsDelete(id);
        return AjaxResult.success();
    }

}
